package algorithm;

import datastructure.Fraction;
import datastructure.Pair;
import java.util.ArrayList;
import java.util.HashMap;

public class NaiveBayes {

  private NaiveBayes() {
  }

  public static NaiveBayesModel generateModel(ArrayList<ArrayList<String>> attribute_list) {
    ArrayList<HashMap<String, Pair<Fraction, Fraction>>> data = new ArrayList<HashMap<String, Pair<Fraction, Fraction>>>();
    int right = 0, wrong = 0;

    for (int row = 0; row < attribute_list.get(0).size(); row++) {
      if ("1".equals(attribute_list.get(0).get(row))) {
        right++;
      } else {
        wrong++;
      }
    }
    Pair<Fraction, Fraction> frac = Pair.makePair(new Fraction(right,attribute_list.get(0).size()), new Fraction(wrong,attribute_list.get(0).size()));

    for (int attribute_number = 1; attribute_number < attribute_list.size(); attribute_number++) {
      HashMap<String, Pair<Fraction,Fraction>> temp;
      int count11 = 0, count10 = 0, count01 = 0, count00 = 0;

      for (int row = 0; row < attribute_list.get(attribute_number).size(); row++) {
        temp = new HashMap<String, Pair<Fraction,Fraction>>();
        if ("1".equals(attribute_list.get(attribute_number).get(row))) {
          if ("1".equals(attribute_list.get(0).get(row))) {
            count11++;
          } else {
            count10++;
          }
        } else {
          if ("1".equals(attribute_list.get(0).get(row))) {
            count01++;
          } else {
            count00++;
          }
        }
        
        Pair<Fraction,Fraction> p = Pair.makePair(new Fraction(count11,right), new Fraction(count10,wrong));
        temp.put("1", p);
        
        p = Pair.makePair(new Fraction(count01,right), new Fraction(count00,wrong));
        temp.put("0", p);
        
        data.add(temp);
      }
      
      return new NaiveBayesModel(data, frac);
    }

    return null;
  }
  
  public static void main (String[] args) {
    
  }
  
}
